package com.sskj.mine.ui.activity;


import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.FeedBackTypeBean;
import com.sskj.mine.presenter.FeedBackActivityPresenter;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;


/**
 * 问题反馈
 */
@Route(path = RConfig.MINE_FEEDBACK)
public class FeedBackActivity extends BaseActivity<FeedBackActivityPresenter> {
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;
    @BindView(R2.id.etDesc)
    EditText etDesc;
    @BindView(R2.id.etPhone)
    EditText etPhone;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private List<FeedBackTypeBean> feedBackTypeBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_activity_feed_back;
    }

    @Override
    public FeedBackActivityPresenter getPresenter() {
        return new FeedBackActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_feedBackActivity1));
    }

    @Override
    protected void initData() {
        mPresenter.getFeedBackType();
    }

    public void updateFeedBackType(List<FeedBackTypeBean> feedBackTypeBeans) {
        this.feedBackTypeBeans = feedBackTypeBeans;
        Flowable.fromIterable(feedBackTypeBeans)
                .map(feedBackTypeBean -> feedBackTypeBean.getType_name())
                .toList()
                .subscribe((strings, throwable) -> {
                    heySpinner.attachData(strings);
                });
        ClickUtil.click(btSubmit, () -> {
            if (checkParam()) {
                mPresenter.feedBack(feedBackTypeBeans.get(heySpinner.getSelectIndex()).getId(), etDesc.getText().toString(), etPhone.getText().toString());
            }
        });

    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(etDesc.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_feedBackActivity2));
            return false;
        }
        if (TextUtils.isEmpty(etPhone.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_feedBackActivity3));
            return false;
        }
        return true;
    }


    public void feedBackSuccess() {
        finish();
    }
}
